<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
<div class="main-content">
    <div class="box box-main">
        <form id="nodeForm" model="post" action="<c:url value="/web/admin/auditFlow/saveNode.action"/>"
              method="post" class="form-horizontal">
            <div class="box-body">
                <div class="form-unit">基本信息</div>
                <input type="hidden" name="id" value="${auditNode.id}"/>
                <input type="hidden" name="flowId" value="${flowId}"/>
                <input type="hidden" name="nodeTypeEnum" id="nodeTypeEnum" value="${auditNode.nodeTypeEnum}"/>
                <input type="hidden" name="parentId" value="${auditNode.parentId}"/>
                <input type="hidden" name="createDate"
                       value="<fmt:formatDate value="${auditNode.createDate}" pattern="yyyy-MM-dd HH:mm:ss"/>"/>
                <input type="hidden" name="parent" value="${parentId}"/>
                <div class="row">
                    <div class="col-xs-12">
                        <div class="form-group">
                            <label class="control-label col-sm-4" title="">
                                <span class="required">*</span>名称：<i class="fa icon-question hide"></i></label>
                            <div class="col-sm-8">
                                <input path="name" name="name" id="name" value="${auditNode.nodeName}"
                                       class="form-control required">
                            </div>
                        </div>
                    </div>
                    <div class="col-xs-12">
                        <div class="form-group">
                            <label class="control-label col-sm-4" title="">
                                <span class="required ">*</span> 指定类型：<i class="fa icon-question hide"></i></label>
                            <div class="col-sm-8">
                                <select id="type" name="type" class="form-control required">
                                <c:forEach items="${nodeType}" var="type">
                                    <c:if test="${type=='SELF'}"><option value="${type}" <c:if test="${auditNode.nodeTypeEnum=='SELF'}"> selected</c:if>>个人</option></c:if>
                                    <c:if test="${type=='USER'}"> <option value="${type}" <c:if test="${auditNode.nodeTypeEnum=='USER'}"> selected</c:if>>人员</option></c:if>
                                    <c:if test="${type=='POSITION'}"> <option value="${type}" <c:if test="${auditNode.nodeTypeEnum=='POSITION'}"> selected</c:if>>岗位</option></c:if>
                                </c:forEach>
                                </select>
                            </div>
                        </div>
                    </div>
                    <div class="col-xs-12" id="organization">
                        <div class="form-group">
                            <label class="control-label col-sm-4" title="">
                                <span class="required ">*</span>选择部门：<i class="fa icon-question hide"></i></label>
                            <div class="col-sm-8">
                                <input path="select_organization" id="select_organization" name="select_organization"
                                       class="form-control required">
                            </div>
                        </div>
                    </div>
                    <div class="col-xs-12" id="position">
                        <div class="form-group">
                            <label class="control-label col-sm-4" title="">
                                <span class="required hide">*</span>选择岗位：<i class="fa icon-question hide"></i></label>
                            <div class="col-sm-8">
                                <div class="ibox tabs-container ibox-content">
                                    <input path="select_position" id="select_position" name="select_position"
                                           class="form-control required">
                                </div>
                            </div>
                        </div>
                    </div>
                    <div class="col-xs-12" id="user">
                        <div class="form-group">
                            <label class="control-label col-sm-4" title="">
                                <span class="required hide">*</span>选择人员：<i class="fa icon-question hide"></i></label>
                            <div class="col-sm-8">
                                <div class="ibox tabs-container ibox-content">
                                    <input path="select_user" id="select_user" name="select_user"
                                           class="form-control required">
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
            <div class="box-footer">
                <div class="row">
                    <div class="col-sm-offset-2 col-sm-10">
                        <button type="submit" class="btn btn-sm btn-primary" id="btnSubmit"><i class="fa fa-check"></i>
                            保 存
                        </button>&nbsp;

                        <button type="button" class="btn btn-sm btn-default" id="btnCancel" onclick="closeView()"><i
                                class="fa fa-times"></i> 关 闭
                        </button>
                    </div>
                </div>
            </div>
        </form>
    </div>
</div>
<script>
    var user = "";
    var position = "";
    var select_organization = "";
    var select_position = "";
    var select_user = "";
    $("#nodeForm").validate({
        submitHandler: function (form) {
            if($("#nodeType").val()=="POSITION"){
                if( select_organization.getValue() == ""){
                    js.showMessage("请选择岗位", "提示", "warning");
                    return false;
                }
                if( select_position.getValue() == ""){
                    js.showMessage("请选择岗位", "提示", "warning");
                    return false;
                }
            }
            if($("#nodeType").val()=="USER"){
                if( select_organization.getValue() == ""){
                    js.showMessage("请选择岗位", "提示", "warning");
                    return false;
                }
                if( select_user.getValue() == ""){
                    js.showMessage("请选择人员", "提示", "warning");
                    return false;
                }
            }
            js.ajaxSubmitForm($(form), function (data) {
                js.showMessage(data.msg);
                if (data.status == Global.NO) {
                    closeView();
                    window.location.href = "<c:url value="/web/admin/auditFlow/editNodePage.action?id="/>" +${flowId}
                }
            }, "json");
        }
    });

    $(document).ready(function () {
        if( $("#type").val()=="SELF"){
            $("#user").hide();
            $("#position").hide();
            $("#organization").hide();
        }else if($("#type").val()=="USER"){
            $("#position").hide();

        }else if($("#type").val()=="POSITION"){
            $("#user").hide();
        }
        //部门下拉选择
        select_organization = $('#select_organization').magicSuggest({
            allowFreeEntries: false,
            data: '<c:url value="/web/admin/organization/getOrganizationListMagic.action?id=${auditNode.id}" />',
            queryParam: 'q',
            valueField: "value",
            displayField: "text",
            maxSelection: 1,
            noSuggestionText: '',
            placeholder: '请选择',
            required: true,
            autoSelect: true,
            selectFirst: true,
            value: magicSuggestValue('${auditNode.organizationId}')
        });
        getPosition();
        getUser();
    });

    //岗位下拉选择
    function getPosition() {
        select_position = $('#select_position').magicSuggest({
            allowFreeEntries: false,
            data: '<c:url value="/web/admin/position/getPositionListMagic.action?id=${auditNode.relationId}" />',
            queryParam: 'q',
            valueField: "value",
            displayField: "text",
            maxSelection: 1,
            noSuggestionText: '',
            placeholder: '请选择',
            required: true,
            autoSelect: true,
            selectFirst: true,
            value: magicSuggestValue(${auditNode.relationId})
        });
        $(select_position).on('beforeload', function (e, m) {
            if (select_organization.getValue() == "") {
                js.showMessage("请选择部门！");
            } else {
                select_position.setDataUrlParams({"organizationId": select_organization.getValue() + ""});
            }
        });
    }


    //人员下拉选择
    function getUser() {
        select_user = $('#select_user').magicSuggest({
            allowFreeEntries: false,
            data: '<c:url value="/web/admin/systemUser/getUserList.action?id=${auditNode.relationId}" />',
            queryParam: 'q',
            valueField: "value",
            displayField: "text",
            maxSelection: 1,
            noSuggestionText: '',
            placeholder: '请选择',
            required: true,
            autoSelect: true,
            selectFirst: true,
            value: magicSuggestValue(${auditNode.relationId})
        });
    }

    $(select_user).on('beforeload', function (e, m) {
        if (select_organization.getValue() == "") {
            js.showMessage("请选择部门！");
        } else {
            select_user.setDataUrlParams({"organizationId": select_organization.getValue() + ""});
        }
    });


    function closeView() {
        layer.close(LayWin['wNewNode']);

    }

    $("#type").change(function (){
        if($("#type").val()=="SELF"){
            $("#user").hide();
            $("#position").hide();
            $("#organization").hide();
        }else if($("#type").val()=="USER"){
            $("#organization").show();
            $("#user").show();
            $("#position").hide();

        }else if($("#type").val()=="POSITION"){
            $("#organization").show();
            $("#user").hide();
            $("#position").show();
        }
    })
</script>
